今天要做的是讓圖片可以放大縮小
但是使用Xib建立的還在研究當中@@
先來看看Code的建置,參考網路上的大神做的
class ShowImageView: UIScrollView {
private var imageView = UIImageView()
override init(frame: CGRect) {
super.init(frame: frame)
initXib()
baseInit()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
initXib()
baseInit()
}
override func awakeFromNib() {
initXib()
}
func baseInit() {
imageView.image = UIImage(systemName: "person")
imageView.contentMode = .scaleAspectFill
addSubview(imageView)
self.bringSubviewToFront(imageView)
imageView.snp.makeConstraints { make in
make.top.equalTo(self.snp.top)
make.left.equalTo(self.snp.left)
make.right.equalTo(self.snp.right)
make.bottom.equalTo(self.snp.bottom)
}
minimumZoomScale = 1
maximumZoomScale = 3
delegate = self
}
private func initXib() {
let view = Bundle.main.loadNibNamed(String(describing: ShowImageView.self), owner: self, options: nil)?.first as! UIView
addSubview(view)
view.frame = self.bounds
}
extension ShowImageView: UIScrollViewDelegate {
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return imageView
}
}
https://gist.github.com/jonnytownend/0215af647596117960ef9a15ec7e39fa?file=PanZoomImageView-blog-1.swift
https://medium.com/%E5%BD%BC%E5%BE%97%E6%BD%98%E7%9A%84-swift-ios-app-%E9%96%8B%E7%99%BC%E5%95%8F%E9%A1%8C%E8%A7%A3%E7%AD%94%E9%9B%86/%E5%9C%A8-storyboard-%E5%8A%A0%E5%85%A5-xib-%E7%9A%84-view-e94826a7a8f3